package api

import (
	"gitee.com/kevin186/keyauth/apps/menu"
	"gitee.com/kevin186/keyauth/apps/token"
	restfulspec "github.com/emicklei/go-restful-openapi"
	"github.com/emicklei/go-restful/v3"
	"github.com/infraboard/mcube/app"
	"github.com/infraboard/mcube/http/label"
	"github.com/infraboard/mcube/http/response"
	"github.com/infraboard/mcube/logger"
	"github.com/infraboard/mcube/logger/zap"

	"gitee.com/kevin186/keyauth/apps/user"
)

var (
	h = &handler{}
)

type handler struct {
	service      menu.ServiceServer
	tokenService token.ServiceServer
	log          logger.Logger
}

func (h *handler) Config() error {
	h.log = zap.L().Named(menu.AppName)
	h.service = app.GetGrpcApp(menu.AppName).(menu.ServiceServer)
	h.tokenService = app.GetGrpcApp(token.AppName).(token.ServiceServer)
	return nil
}

func (h *handler) Name() string {
	return menu.AppName
}

func (h *handler) Version() string {
	return "v1"
}

func (h *handler) Registry(ws *restful.WebService) {
	tags := []string{"menus"}

	//ws.Route(ws.POST("login").To(h.Login).
	//	Doc("login").
	//	// 是否开启认证
	//	Metadata(label.Auth, label.Enable).
	//	// 是否开启鉴权
	//	Metadata(label.Permission, label.Enable).
	//	// 开启行为审计
	//	Metadata(label.Audit, label.Enable).
	//	Metadata(restfulspec.KeyOpenAPITags, tags).
	//	Reads(user.CreateUserRequest{}).
	//	Writes(response.NewData(user.User{})))

	ws.Route(ws.POST("getinfo").To(h.GetInfo).
		Doc("getinfo").
		// 是否开启认证
		Metadata(label.Auth, label.Enable).
		// 是否开启鉴权
		Metadata(label.Permission, label.Enable).
		// 开启行为审计
		Metadata(label.Audit, label.Enable).
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Reads(user.CreateUserRequest{}).
		Writes(response.NewData(user.User{})))

	ws.Route(ws.POST("").To(h.CreateMenu).
		Doc("create a menu").
		Metadata(restfulspec.KeyOpenAPITags, tags).
		Reads(user.CreateUserRequest{}).
		Writes(response.NewData(user.User{})))

	//ws.Route(ws.GET("/").To(h.QueryMenu).
	//	Doc("get all users").
	//	Metadata(restfulspec.KeyOpenAPITags, tags).
	//	Metadata("action", "list").
	//	Reads(user.CreateUserRequest{}).
	//	Writes(response.NewData(user.UserSet{})).
	//	Returns(200, "OK", user.UserSet{}))
	//
	//ws.Route(ws.GET("/{id}").To(h.DescribeMenu).
	//	Doc("get a user").
	//	Param(ws.PathParameter("id", "identifier of the user").DataType("integer").DefaultValue("1")).
	//	Metadata(restfulspec.KeyOpenAPITags, tags).
	//	Writes(response.NewData(user.User{})).
	//	Returns(200, "OK", response.NewData(user.User{})).
	//	Returns(404, "Not Found", nil))
	//
	//ws.Route(ws.PUT("/{id}").To(h.UpdateMenu).
	//	Doc("update a user").
	//	Param(ws.PathParameter("id", "identifier of the user").DataType("string")).
	//	Metadata(restfulspec.KeyOpenAPITags, tags).
	//	Reads(user.CreateUserRequest{}))
	//
	//ws.Route(ws.PATCH("/{id}").To(h.PatchMenu).
	//	Doc("patch a user").
	//	Param(ws.PathParameter("id", "identifier of the user").DataType("string")).
	//	Metadata(restfulspec.KeyOpenAPITags, tags).
	//	Reads(user.CreateUserRequest{}))
	//
	//ws.Route(ws.DELETE("/{id}").To(h.DeleteMenu).
	//	Doc("delete a user").
	//	Metadata(restfulspec.KeyOpenAPITags, tags).
	//	Param(ws.PathParameter("id", "identifier of the user").DataType("string")))
}

func init() {
	app.RegistryRESTfulApp(h)
}
